﻿#region Copyright
/**
 * Copyright (c) 2010 EPiServer AB
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software 
 * and associated documentation files (the "Software"), to deal in the Software without restriction,
 * including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, 
 * and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, 
 * subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all copies or substantial 
 * portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 
 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE 
 * AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 
 **/
#endregion

using System;
using System.Web.UI.WebControls;
using EPiServer.Common.Queries;
using EPiServer.Community.Contest;
using EPiServer.Community.Contest.Queries;

namespace EPiXternal.RelatePlus.SpecializedProperties.ContestPicker
{
    public partial class ContestBrowser : RelateEntityBrowserBase
    {
        #region Method overrides
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            if (!IsPostBack)
            {
                BindSearchContests(0, dgEntities.PageSize);
                hdnEntityId.Value = EntityValue;
                epiBtnOk.OnClientClick = String.Format("onOk('{0}')", hdnEntityId.ClientID);
            }
        }
        #endregion

        #region Event handlers
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            BindSearchContests(0, dgEntities.PageSize);
        }

        protected void dgEntities_PageIndexChanged(object sender, DataGridPageChangedEventArgs e)
        {
            BindSearchContests(e.NewPageIndex, dgEntities.PageSize);
        }

        protected void dgEntities_ItemDataBound(object sender, DataGridItemEventArgs e)
        {
            if (e.Item != null && e.Item.DataItem != null)
            {
                e.Item.Attributes.Add("id", String.Format("row_{0}", ((EPiServer.Community.Contest.Contest)e.Item.DataItem).ID.ToString()));

                if(((EPiServer.Community.Contest.Contest)e.Item.DataItem).ID.ToString() == EntityValue)
                {
                    e.Item.Attributes.Add("class", "epixternal-selectedRow");
                }
            }
        }
        #endregion

        #region Helpers
        private void BindSearchContests(int pageIndex, int pageSize)
        {
            int totalItems;

            ContestQuery query = new ContestQuery();
            query.Name = new StringCriterion();
            query.Name.Value = txtName.Text.TrimEnd();
            query.Name.WildCardType = WildCardType.Both;

            ContestCollection contests = QueryHandler.GetQueryResult<Contest, ContestCollection>(query, pageIndex + 1, pageSize, out totalItems);

            litNumberOfHits.Text = String.Format("Number of hits: {0}", totalItems);

            dgEntities.DataSource = contests;
            dgEntities.VirtualItemCount = totalItems;
            dgEntities.CurrentPageIndex = pageIndex;
            dgEntities.DataBind();
        }

        #endregion
    }
}